<?php
include_once(PATH_APP_COMMON . "AuthManager.php");
class Controller_AdminSpecialsMeetings extends Controller_AdminList  {
	public $tabs;

	function __construct()
	{

		parent::__construct();

		$id =  _gp('id','int');
		$tabs = array(
			"基本信息"  => $this->url("Info","id=".$id),
			"Flash图片" => $this->url("Flashnews","id=".$id),
			"添加分论坛" => $this->url("ForumsCreate","id=".$id),
			"分论坛管理" => $this->url("ForumsIndex","id=".$id),
			"新闻管理" => $this->url("News","id=".$id),
		);

		$this->assign('tabs',$tabs);
	}

	public function actionInfo(){
		$this->setBack();
		$pkv =  _gp('id','int');
		$name = M('Specials')->selectVar('name','where id = ' . $pkv);
		$data = $this->be($pkv);
		$data['id'] = $pkv;
		$data['name'] = $name;
		$this->assign("data",$data);
		$this->display();
	}

	public function actionFlashNews(){
		$this->setBack();
		$pkv =  _gp('id','int');
		$name = M('Specials')->selectVar('name','where id = ' . $pkv);
		$data = $this->be($pkv);
		$data['id'] = $pkv;
		$data['name'] = $name;
		$data['flash_news'] = unserialize($data['flash_news']);

		$this->assign("data",$data);
		$this->display();
	}

	public function actionSaveFlashNews(){

		$pkv =  _gp('id','int');
		$flash['titles'] = _gp("title",'arr');
		$flash['imgs'] = _gp("img",'arr');
		$flash['urls'] = _gp("url",'arr');

		$data['id'] = $pkv;
		$data['flash_news'] = serialize($flash);

		$this->save($data);
		$this->success(MESSAGE_SUCCESS);
	}
	public function actionSave(){
		//如果数据库已存在记录则更新，否则添加
		if (isset($_POST['name'])) {
			$data['name'] = _gp('name','string');
			$data['id'] = _gp('id','int');
			M('Specials')->save($data);
		}
		$data = $_POST;
		
		
		$allowExts = 'jpg,png,gif';
		$maxSize = 2000 * 1024; // 300KB
		$uploadDir = PATH_APP_UPLOAD . 'specials/';
		
		$uploader = new Helper_FileUploader();
		$files =& $uploader->getFiles();
		
		$file = current($files);
		
		if ($file && !$file->check($allowExts, $maxSize)) {
			// 上传的文件类型不符或者超过了大小限制。
			_message("你上传的文件类型不符或者超过了大小限制。");
			return false;
		}
		
		$file && $data['ext'] = $file->getExt();
		if (!empty($file)) {
			$filename = md5(time() . $file->getFilename() . $file->getSize() . $file->getTmpName()) . '.' . strtolower($file->getExt());
			$file->move($uploadDir . $filename);
			$data['top_banner'] = $filename;
		}
		
		$this->save($data);
		$this->success(MESSAGE_SUCCESS);
	}


	public function actionForumsCreate(){

		/* @var $forums Model_SpecialsMeetingsForums */
		$forums = M('SpecialsMeetingsForums');


		$this->setBack();
		$mid =  _gp('id','int');
		$name = M('Specials')->selectVar('name','where id = ' . $mid);

		$data['mid'] = $mid;

		$data['detail'] = file_get_contents(dirname(__FILE__) . '/AdminSpecialsMeetingsForumExample.php');

		$this->assign("data",$data);
		$this->assign("pk",$forums->getPkName());

		$this->display();
	}

	public function actionForumsEdit(){

		/* @var $forums Model_SpecialsMeetingsForums */
		$forums = M('SpecialsMeetingsForums');
		$pk = $forums->getPkName();


		$this->setBack();
		$pkv =  _gp($pk,'int');

		$data = M('SpecialsMeetingsForums')->be($pkv);

		$mid = $data['mid'];
		$name = M('Specials')->selectVar('name','where id = ' . $mid);

		$this->assign("data",$data);
		$this->assign("pk",$pk);
		$this->display('SpecialsMeetings_ForumsCreate');
	}

	public function actionForumsIndex(){

		$this->setBack();
		$mid =  _gp('id','int');
		$name = M('Specials')->selectVar('name','where id = ' . $mid);
		/* @var $forums Model_SpecialsMeetingsForums */
		$forums = M('SpecialsMeetingsForums');
		$data = $forums->select('*',"order by `order` asc ,fid desc");

		$this->assign("data",$data);
		$this->assign("id",$mid);
		$this->assign("pk",$forums->getPkName());
		$this->display();
	}
	public function actionForumsSave(){
		//如果数据库已存在记录则更新，否则添加
		M('SpecialsMeetingsForums')->save($_POST);
		$this->success(MESSAGE_SUCCESS);
	}
	public function actionForumsRemove(){
		$fid =  _gp('fid','int');
		M('SpecialsMeetingsForums')->remove($fid);
		$this->success(MESSAGE_SUCCESS);
	}
	public	function actionForumsBatch(){
		$data = $_POST['data'];
		$do = strtolower(_gp("do",'string'));
		$pkvs = _gp("pkv", 'array');

		/* @var $forums Model_SpecialsMeetingsForums */
		$model = M('SpecialsMeetingsForums');

		$pk = $model->getPkName();

		switch ($do){
			case "order":
				foreach ($data as $id => $row){
					$row[$pk] = $id;
					$model->save($row);
				}
				break;
			case "remove":
				$model->remove($pkvs);
				break;
		}
		$this->success(MESSAGE_SUCCESS);
	}
}
